<template>
    <div id="subtmpl">
        <div class="inright div" @click="substrict">-</div>
        <div  class="incenter div" v-text="count"></div>
        <div class="inleft div" @click="add">+</div>
    </div>
</template>

<script>


    export default {
        props:['initCount', 'goodsid'],
        data() {
            return {
                count:1,
                resObj: {
                    goodsid:0,
                    type:'add'
                }
            }
        },
        created() {
            this.count = this.initCount;
        },
        methods: {
            add() {
                this.count++;
                this.sendmessage("add");
            },
            substrict() {
                this.count--;
                //确保最小值为1
                if (this.count < 1) {
                    this.count = 1;
                    return;
                }
                this.sendmessage("substrict");
            },
            sendmessage(type) {//注意时this.count，让父组件注册监听carobj
                this.resObj.type = type;
                this.resObj.goodsid = this.goodsid;
                this.$emit('carobj', this.resObj);

            }
        }
    }
</script>

<style scoped>
    .inleft, .inright, .incenter{
        width: 40px;
        height: 25px;
        border: 1px solid #000;
        display: inline-block;
        text-align: center;
        line-height: 20px;
    }
</style>